import { createRouter, createWebHashHistory } from 'vue-router';
import { login, logout } from '../assets/login/tologin';
import store from "../store";

const router = createRouter({
  history: createWebHashHistory(), // hash模式：createWebHashHistory，history模式：createWebHistory
  routes: [
    {
      path: '/',
      redirect: '/dashboard'
    },
    {
      path: '/introduce',
      name: 'introduce',
      meta: {
        type: 'introduce'
      },
      component: () => import(/* webpackChunkName: "introduce" */ '../views/Introduce.vue')
    },
    {
      path: '/dashboard',
      name: 'dashboard',
      meta: {
        type: 'dashboard'
      },
      component: () => import(/* webpackChunkName: "dashboard" */ '../views/Index.vue')
    },
    {
      path: '/login',
      name: 'login',
      component: () => import(/* webpackChunkName: "login" */ '../views/Login.vue')
    },
    {
      path: '/add',
      name: 'mbtrialOpen',
      meta: {
        type: "mbtrialOpen"
      },
      component: () => import(/* webpackChunkName: "add" */ '../views/AddGood.vue')
    },
    {
      path: '/swiper',
      name: 'swiper',
      component: () => import(/* webpackChunkName: "swiper" */ '../views/Swiper.vue')
    },
    {
      path: '/hot',
      name: 'hot',
      component: () => import(/* webpackChunkName: "hot" */ '../views/IndexConfig.vue')
    },
    {
      path: '/new',
      name: 'new',
      component: () => import(/* webpackChunkName: "new" */ '../views/IndexConfig.vue')
    },
    {
      path: '/recommend',
      name: 'recommend',
      component: () => import(/* webpackChunkName: "recommend" */ '../views/IndexConfig.vue')
    },
    {
      path: '/category',
      name: 'mbtargetEntry',
      meta: {
        type: 'mbtargetEntry'
      },
      component: () => import(/* webpackChunkName: "category" */ '../views/Category.vue'),
      children: [
        {
          path: 'level',
          name: 'level',
          component: () => import(/* webpackChunkName: "level3" */ '../views/AddGood.vue'),
        }
      ]
    },
    
    {
      path: '/good',
      name: 'mbtargetFirstTrial',
      meta: {
        type: 'mbtargetFirstTrial'
      },
      component: () => import(/* webpackChunkName: "new" */ '../views/Good.vue')
    },
    {
      path: '/guest',
      name: 'guest',
      component: () => import(/* webpackChunkName: "guest" */ '../views/Guest.vue')
    },
    {
      path: '/order',
      name: 'order',
      component: () => import(/* webpackChunkName: "order" */ '../views/Order.vue')
    },
    {
      path: '/order_detail',
      name: 'order_detail',
      component: () => import(/* webpackChunkName: "order_detail" */ '../views/OrderDetail.vue')
    },
    {
      path: '/account',
      name: 'account',
      component: () => import(/* webpackChunkName: "account" */ '../views/Account.vue')
    },
    {
      path: "/errorinfo",
      name: "errorinfo",
      component: () => import(/* webpackChunkName: "account" */ '../components/error.vue')
    }
  ]
})


router.beforeEach((to, from, next) => {
  if (localStorage.getItem("pageName") == to.meta.type) {
    localStorage.setItem("console", "don not look!");
  } else {
    localStorage.setItem("pageName", to.meta.type);
    localStorage.removeItem(to.name + "pageNo");
    localStorage.removeItem(to.name + "pageSize");
  }
  if (!localStorage.getItem('userinfo') || !JSON.parse(localStorage.getItem('userinfo')).logined) {
    login()
  }
  store.commit("cart/GETUSERINFO");
  if (to.matched.length == 2){
    to.matched.splice(0,1);
  }
  if (to.matched.length === 0) {
  // 判断此跳转路由的来源路由是否存在，存在的情况跳转到来源路由，否则跳转到404页面
    next("/errorinfo");
  } else {
    next();
  }
})

export default router